// component/filterControl/filterControl.js
import api from './../../common/api'

Component({
  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的传入样式
   */
  externalClasses: ['style-extra'],

  /**
   * 组件的属性列表
   */
  properties: {
    cityCode: {
      type: String,
      value: ''
    },
    filterValue: {
      type: String,
      value: ''
    },
    filterSelect: {
      type: Array,
      value: [],
    },
    filterList: {
      type: Array,
      value: [],
    },
    filterLabel: {
      type: String,
      value: ''
    },
    filterId: {
      type: String,
      value: ''
    },
    popupType: {
      type: String,
      value: 'row'
    },
    isCustomHeader: {
      type: String,
      value: '0',
    },
    maxWidth: {
      type: Number,
      value: 110,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isShowPopup: false,
  },

  /**
   * 组件的数据监听
   */
  observers: {
    cityCode() {
      let t = this
      if (t.data.filterId === 'district') {
        t.loadCityDistrictList()
      }
    }
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
      let t = this
      if (t.data.popupType === 'column') {
        t.setData({
          filterSelect: [0, 0, 0]
        })
      }
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 页面的生命周期
   */
  pageLifetimes: {
    show: function () {},
    hide: function () {},
    resize: function (size) {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    resetFilterData() {
      this.setData({
        filterValue: '',
        filterSelect: this.data.popupType === 'column' ? [0, 0, 0] : []
      })
    },
    onClickLabel() {
      let t = this
      t.setData({
        isShowPopup: !t.data.isShowPopup
      })
    },

    onPopupClose() {
      let t = this
      t.setData({
        isShowPopup: false
      })
    },

    onPopupConfirm(e) {
      let t = this
      let {
        select,
        result,
        label
      } = e.detail || {}
      if (label === '不限') {
        label = ''
      }
      t.setData({
        isShowPopup: false,
        filterSelect: select,
        filterValue: label
      })
      t.triggerEvent('filterChange', {
        target: t.data.filterId,
        result,
        select,
      })
    },

    loadCityDistrictList() {
      let t = this
      let {
        cityCode
      } = t.data || {}
      if (!cityCode) {
        return
      }
      api.getAreaList({}, cityCode, (data) => {
        let list = data.data.content || []
        let result = list.map(item => {
          return {
            label: item.areaName || '',
            value: item.areaName || '',
            code: item.areaCode || '',
            id: item.areaId || ''
          }
        })
        result.unshift({
          label: '不限',
          value: '',
          code: '',
          id: ''
        })
        t.setData({
          'filterList[1]': [...result],
        });
      });
    },

    isWhatType(o) {
      return Object.prototype.toString.call(o).slice(8, -1)
    }

  }

})